package com.random.fox.whatspay.greendao.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.random.fox.whatspay.bean.Account;
import com.random.fox.whatspay.bean.Unit;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by 渠 on 2014/11/9.
 */
public class AccountDao extends AbstractDao<Account, Long> {

    public static final String TABLENAME = "account";

    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Name = new Property(1, String.class, "name", false, "NAME");
    };

    public AccountDao(DaoConfig config) {
        super(config);
    }

    public AccountDao(DaoConfig config, AbstractDaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'account' (" + //
                "'_id' INTEGER PRIMARY KEY AUTOINCREMENT," + // 0: id
                "'NAME' TEXT NOT NULL )"); // 1: name
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'account'";
        db.execSQL(sql);
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, Account account) {
        stmt.clearBindings();

//        Long id  = account.getId();
//        if (id != null){
//            stmt.bindLong(1,id+1);
//        }
        stmt.bindString(2,account.getName());

    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    protected Account readEntity(Cursor cursor, int offset) {
        Account account = new Account(Unit.CHINA);
        account.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); // id
        account.setName(cursor.getString(offset + 1)); //name
        return account;
    }

    @Override
    protected void readEntity(Cursor cursor, Account account, int offset) {
        account.setId(cursor.isNull(offset+0)?null:cursor.getLong(offset+0));
        account.setName(cursor.getString(offset + 1)); //name
    }

    @Override
    protected Long updateKeyAfterInsert(Account account, long rowId) {
        account.setId(rowId);
        return rowId;
    }

    @Override
    protected Long getKey(Account account) {
        if(account != null) {
            return account.getId();
        } else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }
}
